//
//  LS_StatusPictureView.swift
//  weibo
//
//  Created by 罗李苏 on 16/5/25.
//  Copyright © 2016年 Apple. All rights reserved.
//显示微博配图的视图

import UIKit
//图片之间的间距
private let pictureMargin: CGFloat = 3
//最大宽度
private let maxWidth = ScreenWidth - 2 * StatusCellMargin
//单张图片的宽度
private let itemWidth = (maxWidth - 2 * pictureMargin) / 3
//cell的重用标示
private let pictureCellId = "pictureCellId"

class LS_StatusPictureView: UICollectionView {
    //数据源数组
    var pictures:[LS_Picture]?{
        
        didSet{
            testLabel.text = "\(pictures?.count ?? 0)"
            //根据图片多少 更新 大小的 约束
            let pSize = calculatePictureViewSize()
//            self.snp_makeConstraints { (make) in
//                make.size.equalTo(pSize)
//            }
            self.snp_updateConstraints { (make) in
                make.size.equalTo(pSize)
            }
            //刷新数据源
            reloadData()
        
        }
    
    }
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        //注意必须实例化一个 layout 不然系统会崩溃
        let layout = UICollectionViewFlowLayout()
        //设置间距
        layout.minimumLineSpacing = pictureMargin
        layout.minimumInteritemSpacing = pictureMargin
        super.init(frame: frame, collectionViewLayout: layout)
        //设置数据源
        self.dataSource = self
        //不响应点击状态栏返回最上面那一行cell
        scrollsToTop = false
        //注册cell
        registerClass(LS_StatusPictureCell.self, forCellWithReuseIdentifier: pictureCellId)
        
        setupUI()
           }
    
    //MARK:加载测试用的label
    private func setupUI(){
        addSubview(testLabel)
        //设置约束
        testLabel.snp_makeConstraints { (make) in
            make.center.equalTo(self.snp_center)
        }
    
    }
    //懒加载一个UILabel 测试用
    private lazy var testLabel: UILabel = UILabel(title: "这是Label", font: 14, textColor: randomColor(), alignment: .Center)

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //需要根据配图视图的显示的数据的 '数量' 来确定配图视图的大小
    /*
     1. 单张图片按照图片的原比例来显示
     2. 四张图片按照 2 * 2 对称显示
     3. 其他的多张图片 n * 3 形式来显示
     */
    //MARK:计算配图视图的大小
    func calculatePictureViewSize() -> CGSize{
        //先获取有多少张图
        let imageCount = pictures?.count ?? 0
        //如果没有配图
        if imageCount == 0 {
            return CGSizeZero
        }
        
        //获取collectioView的布局对象
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        //如果有一张配图
        if imageCount == 1 {
            //图片原比例显示
            //TODO:后面完善
            let imageSize = CGSizeMake(100, 150)
            //还需要设置itemSize 和 图片一样大
            layout.itemSize = imageSize
            return imageSize
        }
        //设置其他多张图片的itemSize
        layout.itemSize = CGSizeMake(itemWidth, itemWidth)
        //如果有四张配图
        if imageCount == 4 {
            let w = itemWidth * 2 + pictureMargin
            return CGSizeMake(w, w)
            
        }
        
        //如果有其他多张图片
        /*
         1,2,3
         4,5,6
         7,8,9
         */
        //计算有几行
        let row = CGFloat((imageCount - 1) / 3) + 1
        //所有图片的最大高度
        let maxHeight = row * itemWidth + (row - 1) * pictureMargin
        return CGSizeMake(maxWidth, maxHeight)
        
    }
    
}
//MARK:数据源方法
extension LS_StatusPictureView: UICollectionViewDataSource{
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return pictures?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(pictureCellId, forIndexPath: indexPath) as! LS_StatusPictureCell
//        cell.backgroundView = randomColor()
        
        //给cell的模型赋值
        cell.picture = pictures![indexPath.item]
        
        return cell
    }
}
//MARK:自定义 LS_StatusPictureView 使用的 cell
//因为要使用的cell功能很少只是展示图片 所以没有专门 建立一个类文件来描述它
class LS_StatusPictureCell: UICollectionViewCell {
    //给 cell 设置模型
    var picture: LS_Picture? {
        
        didSet{
            let url = NSURL(string: picture?.thumbnail_pic ?? "")
            imageView.sd_setImageWithURL(url)
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    private func setupUI(){
        //添加子控件
       contentView.addSubview(imageView)
        
        //添加约束
        //设置约束
        imageView.snp_makeConstraints { (make) in
            make.edges.equalTo(contentView.snp_edges)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //懒加载子控件
    private lazy var imageView: UIImageView = {
        let iv = UIImageView()
        iv.contentMode = .ScaleAspectFill
        //手写代码需要自动设置裁剪 xib/sb会自动设置裁剪
        iv.clipsToBounds = true
        
        return iv
    
    }()
}

//懒加载一个UILabel 测试用
//private lazy var testLabel: UILabel = UILabel(title: "", font: 14, textColor: randomColor(), alignment: .Center)
